package com.tech.view.panel;

import com.tech.repository.dto.GoodsInfo;
import com.tech.service.GoodsService;
import com.tech.service.impl.ServiceImplFactory;
import com.tech.util.LogProcessUtil;
import com.tech.view.frame.MainFrame;
import org.jdesktop.animation.transitions.ScreenTransition;
import org.jdesktop.animation.transitions.TransitionTarget;
import org.jdesktop.core.animation.timing.Animator;
import org.jdesktop.core.animation.timing.interpolators.AccelerationInterpolator;
import org.jdesktop.swingx.WrapLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * 商品推荐面板
 */
public class HOTPanel extends JPanel implements TransitionTarget {
	private final static Logger LOG = LogProcessUtil.getLogger(MainFrame.class.getName());
	private GoodsService goodsService;
	private MainFrame frame;

	// 视图
	private List<GoodsCardPanel> displayPanels;
	private String keyword = "";

	private ScreenTransition transition;

	public HOTPanel(MainFrame frame) {
		this.frame = frame;
		initService();
		initComponents();
		setListeners();
		initView();
	}

	private void initService() {
		goodsService = ServiceImplFactory.getGoodsService();
	}

	private void initComponents() {
		// TODO 读取商品图片数据速度太慢，添加线程来处理该任务
		List<GoodsInfo> goods = goodsService.getRecommendGoods(0, 30);
		displayPanels = new ArrayList<>();
		for (GoodsInfo good : goods) {
			GoodsCardPanel goodsCardPanel = new GoodsCardPanel(good);
			displayPanels.add(goodsCardPanel);
		}
	}

	private void initView() {
		// 动画过渡
		Animator animator = new Animator.Builder()
				.setInterpolator(new AccelerationInterpolator(0.2f, 0.4f))
				.setDuration(500, TimeUnit.MILLISECONDS)
				.build();
		transition = new ScreenTransition.Builder(this, this)
				.setAnimator(animator)
				.build();

		setLayout(new WrapLayout(FlowLayout.LEFT, 18, 10));
		setupNextScreen();
	}

	private void setListeners() {
		for (GoodsCardPanel goodsCardPanel : displayPanels) {
			goodsCardPanel.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					// 商品详情页面板不要放到滚动面板中
					frame.switchContentPanel(new GoodsDetailPanel(((GoodsCardPanel) e.getSource()).getGoodsInfo(), frame), false);
				}
			});
		}
	}

	// TODO 数据库操作
	public void searchGoods(String keyword) {
		// this.keyword = keyword;
		// displayPanels = goodsCardPanels.stream().filter(goodsCardPanel ->
		// 		goodsCardPanel.getGoodsInfo().getGoods().getGname().contains(keyword)).collect(Collectors.toList());
		// // 如果当前不在首页的话，先切换为首页面板再显示搜索结果
		// if (frame.getCurrentContentPanel() != frame.getIndexPanel())
		// 	frame.switchContentPanel(frame.getIndexPanel());
		// transition.start();
	}

	@Override
	public void setupNextScreen() {
		removeAll();
		for (GoodsCardPanel goodsCardPanel : displayPanels) {
			add(goodsCardPanel);
		}
		// 面板要换行必须同时指定宽高，高由于不好计算，这里直接使用最大值
		// setPreferredSize(new Dimension(860, (displayPanels.size() + 3) / 4 * GoodsCardPanel.MAX_HEIGHT));
	}
}
